package com.example.wangzh.developkit.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.wangzh.developkit.R;


public class TopTitleBar extends RelativeLayout {

	private Context mContext;
	private ImageView iv_title_left;
	private TextView tv_title_center;
	private RelativeLayout rll_title_right, rll_img_or_ckb;
	private TextView tv_title_right;
	private ImageView img_title_right;
	private CheckBox ckb_title_right;
	private String titleText;


	public TopTitleBar(Context context) {
		super(context);
		initView(context, null);
	}

	public TopTitleBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context, attrs);
	}

	private void initView(Context context, AttributeSet attrs) {
		this.mContext = context;
		loadAttrs(attrs);
		LayoutInflater.from(mContext).inflate(R.layout.custom_top_title_bar, this);
		iv_title_left = (ImageView) findViewById(R.id.iv_title_left);
		tv_title_center = (TextView) findViewById(R.id.tv_title_center);
		rll_title_right = (RelativeLayout) findViewById(R.id.rll_title_right);
		tv_title_right = (TextView) findViewById(R.id.tv_title_right);
		img_title_right = (ImageView) findViewById(R.id.img_title_right);
		ckb_title_right = (CheckBox) findViewById(R.id.ckb_title_right);
		rll_img_or_ckb = (RelativeLayout) findViewById(R.id.rll_img_or_ckb);
		if (isInEditMode( )) {
			return;
		}
		if (titleText != null) {
			tv_title_center.setText(titleText);
		}
		iv_title_left.setOnClickListener(new OnClickListener( ) {
			@Override
			public void onClick(View v) {
				if (mContext instanceof Activity) {
					((Activity) mContext).finish( );
				}
			}
		});
	}

	private void loadAttrs(AttributeSet attrs) {
		if (attrs == null) {
			return;
		}
		TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.TopTitleBar);
		titleText = typedArray.getString(R.styleable.TopTitleBar_TopTitleBar_title);
	}

	public void setStateOnlyTitle() {
		iv_title_left.setVisibility(View.INVISIBLE);
		tv_title_center.setVisibility(View.VISIBLE);
		rll_title_right.setVisibility(View.VISIBLE);
	}

	public void setRightText(String str) {
		tv_title_right.setVisibility(View.VISIBLE);
		tv_title_right.setText(str);
	}

	public void setLeftImage(int resId) {
		iv_title_left.setImageResource(resId);
	}

	public void setRightImage(int resId) {
		img_title_right.setVisibility(View.VISIBLE);
		ckb_title_right.setVisibility(View.GONE);
		img_title_right.setImageResource(resId);
		rll_img_or_ckb.setVisibility(VISIBLE);
	}

	public void setRightCkbResource(int resId) {
		img_title_right.setVisibility(View.GONE);
		ckb_title_right.setVisibility(View.VISIBLE);
		ckb_title_right.setButtonDrawable(resId);
		rll_img_or_ckb.setVisibility(VISIBLE);
//		rll_title_right.setOnClickListener(new OnClickListener( ) {
//			@Override
//			public void onClick(View v) {
//				ckb_title_right.setChecked(!ckb_title_right.isChecked( ));
//				if (ckb_title_right.isChecked( )) {
//					tv_title_right.setTextColor(getResources( ).getColor(R.color.yellow));
//				} else {
//					tv_title_right.setTextColor(getResources( ).getColor(R.color.text_color__black));
//				}
//			}
//		});
	}

	public void changeRightCkbState() {
		ckb_title_right.setChecked(!ckb_title_right.isChecked( ));
		if (ckb_title_right.isChecked( )) {
			tv_title_right.setTextColor(getResources( ).getColor(R.color.yellow));
		} else {
			tv_title_right.setTextColor(getResources( ).getColor(R.color.text_color__black));
		}
	}

	public void setRightCkbState(boolean check) {
		ckb_title_right.setChecked(check);
		if (ckb_title_right.isChecked( )) {
			tv_title_right.setTextColor(getResources( ).getColor(R.color.yellow));
		} else {
			tv_title_right.setTextColor(getResources( ).getColor(R.color.text_color__black));
		}
	}


	/**
	 * @param title
	 * @Description: ���ñ�������
	 */
	public void setTitleText(String title) {
		this.titleText = title;
		tv_title_center.setText(title);
	}

	/**
	 * @param listener
	 * @Description: ��߰�ť����¼�
	 */
	public void setBtnLeftOnclickListener(OnClickListener listener) {
		iv_title_left.setOnClickListener(listener);
	}

	public void setRightOnclickListener(OnClickListener listener) {
		rll_title_right.setOnClickListener(listener);
	}

	public void setRightVisibility(int visible) {
		rll_title_right.setVisibility(visible);
	}

	public void setRightTextColor(int color) {
		tv_title_right.setTextColor(color);
	}

}
